package com.liyuanjinglyj.europeancup.widget.widget;

import com.liyuanjinglyj.europeancup.utils.LYJHttps;
import com.liyuanjinglyj.europeancup.widget.controller.FormController;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.FormBindingData;
import ohos.aafwk.ability.ProviderFormInfo;
import ohos.aafwk.content.Intent;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.zson.ZSONArray;
import ohos.utils.zson.ZSONObject;

import java.util.Timer;
import java.util.TimerTask;

/**
 * Form controller implementation.
 */
public class WidgetImpl extends FormController {
    private static final HiLogLabel TAG = new HiLogLabel(HiLog.DEBUG, 0x0, WidgetImpl.class.getName());
    private static final int DEFAULT_DIMENSION_2X2 = 2;
    private String url = "https://harmony-1300376177.cos.ap-shanghai.myqcloud.com/europeancup.json";
    private int size=0;
    public WidgetImpl(Context context, String formName, Integer dimension) {
        super(context, formName, dimension);
    }

    private ZSONArray getData() {
        LYJHttps lyjHttps = new LYJHttps();
        String response = lyjHttps.doGet(url);
        ZSONObject jsonStr = ZSONObject.stringToZSON(response);
        ZSONObject result = jsonStr.getZSONObject("result");
        ZSONArray data = result.getZSONArray("data");
        this.size=data.size();
        return data;
    }

    @Override
    public ProviderFormInfo bindFormData() {
        HiLog.info(TAG, "bind form data");
        ProviderFormInfo providerFormInfo = new ProviderFormInfo();
        if (dimension == DEFAULT_DIMENSION_2X2) {
            ZSONObject zsonObject = new ZSONObject();
            HiLog.info(TAG, "update form data: formId" + ((ZSONObject) (getData().get(0))).getString("img"));
            zsonObject.put("imageUrl", ((ZSONObject) (getData().get(0))).getString("img"));
            zsonObject.put("titleName", ((ZSONObject) (getData().get(0))).getString("title"));
            providerFormInfo.setJsBindingData(new FormBindingData(zsonObject));
        }
        return providerFormInfo;
    }

    @Override
    public void updateFormData(long formId, Object... vars) {
        HiLog.info(TAG, "update form data: formId" + formId);

    }



    @Override
    public void onTriggerFormEvent(long formId, String message) {
        HiLog.info(TAG, "onTriggerFormEvent.");
    }

    @Override
    public Class<? extends AbilitySlice> getRoutePageSlice(Intent intent) {
        HiLog.info(TAG, "set route page slice.");
        ZSONObject zsonObject = ZSONObject.stringToZSON(intent.getStringParam("params"));
        if (zsonObject.getString("message").equals("button")) {
            return WidgetSlice.class;
        }
        return null;
    }
}
